package cn.wolfcode.luowowo.util;

import cn.wolfcode.luowowo.exception.LogicException;
import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 参数断言工具类
 */
public abstract class AssertUtil {

    //判断传入的值是否为null
    public static void hasLength(String value, String msg) {
        if (!StringUtils.hasLength(value)) {
            throw new LogicException(msg);
        }
    }

    //判断密码是否一致
    public static void isEquals(String p1, String p2, String msg) {
        if (p1 == null || p2 == null) {
            throw new LogicException("密码不能为空");
        }
        if (!p1.equals(p2)) {
            throw new LogicException(msg);
        }
    }

    /**
     * 正则表达式检验手机号码是否正确
     *
     * @param phone
     */
    public static void isMobile(String phone, String msg) {
        /**
         * 总结： 目前java手机号码正则表达式有：
         * a :"^1[3|4|5|7|8][0-9]\\d{4,8}$"    一般验证情况下这个就可以了
         * b :"^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$"
         */
        String regex = "^1[3|4|5|7|8][0-9]\\d{4,8}$";
        if (phone.length() != 11) {
            throw new LogicException("手机号应为11位数");
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            if (!isMatch) {
                throw new LogicException(msg);
            }
        }
    }

    public static boolean isMobileNO(String phone) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9])|(14[5,7])| (17[0,1,3,5-8]))\\d{8}$");
        Matcher m = p.matcher(phone);
        return m.matches();
    }

    //电话号码

    public static boolean isPhone(String phone) {
        String check = "^(1[3,5][2,4,5,6,7,8,9]|15[0,8,9,1,7]|188|187)\\d{8}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(phone);
        boolean isMatcher = matcher.matches();
        return isMatcher;
       /* if (isMatcher == false) {
            return "请输入有效的电话号码！";
        } else {
            return ("你的电话号码是：" + phone);
        }*/
    }


}